<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="学号" name="xh">
						<a-input v-model:value="searchFormState.xh" placeholder="请输入学号"  style="width: 100px;"/>
					</a-form-item>
				</a-col>
                <a-col :span="4">
                    <a-form-item label="姓名" name="xm">
                        <a-input v-model:value="searchFormState.xm" placeholder="请输入姓名" style="width: 100px;"/>
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="班级名称" name="xzb">
                        <a-input v-model:value="searchFormState.xzb" placeholder="请输入班级名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="院系名称" name="yxmc">
                        <a-input v-model:value="searchFormState.yxmc" placeholder="请输入院系名称" />
                    </a-form-item>
                </a-col>
                <template v-if="advanced">
                    <a-col :span="4">
                        <a-form-item label="是否在校" name="sfzx">
                            <a-input v-model:value="searchFormState.sfzx" placeholder="请输入是否在校" style="width: 60px;"/>
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="入学年份" name="rxnf">
                            <a-input v-model:value="searchFormState.rxnf" placeholder="请输入入学年份" />
                        </a-form-item>
                    </a-col>
                </template>
                <a-col :span="6">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					<a-button style="margin: 0 8px" @click="syncupd">同步</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                        {{ advanced ? '收起' : '展开' }}
                        <component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
                    </a>
                </a-col>
            </a-row>
        </a-form>
		<div>
			<a-spin :spinning="impUploadLoading">

			</a-spin>
		</div>
		<a-alert v-if="impAlertStatus" type="info" :show-icon="false" banner closable @close="onImpClose" class="mt-3">
			<template #description>
				<p>同步总数：{{ impResultData.totalCount }} 条</p>
				<p>同步成功：{{ impResultData.successCount }} 条</p>
				<div v-if="impResultData.errorCount > 0">
					<p><span class="xn-color-red">失败条数：</span>{{ impResultData.errorCount }} 条</p>
					<a-table :dataSource="impResultErrorDataSource" :columns="impErrorColumns" size="small" />
				</div>
			</template>
		</a-alert>
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.xh"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
<!--           <a-space>
                <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('jwXsjbxxAdd')">
                    <template #icon><plus-outlined /></template>
                    新增
                </a-button>
                <xn-batch-button
                    v-if="hasPerm('jwXsjbxxBatchDelete')"
                    buttonName="批量删除"
                       icon="DeleteOutlined"
                    :selectedRowKeys="selectedRowKeys"
                    @batchCallBack="deleteBatchJwXsjbxx"
                />
            </a-space>-->
            </template>
<!--            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('jwXsjbxxEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['jwXsjbxxEdit', 'jwXsjbxxDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteJwXsjbxx(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('jwXsjbxxDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>-->
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="xsjbxx">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import jwXsjbxxApi from '@/api/biz/jwXsjbxxApi'
    let searchFormState = reactive({sfzx:'是'})
    const searchFormRef = ref()
    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    // 查询区域显示更多控制
    const advanced = ref(false)
    const toggleAdvanced = () => {
        advanced.value = !advanced.value
    }
    const columns = [
		{
			title: '院系名称',
			dataIndex: 'yxmc'
		},
		{
			title: '班级名称',
			dataIndex: 'xzb'
		},
		{
			title: '学号',
			dataIndex: 'xh'
		},
        {
            title: '姓名',
            dataIndex: 'xm'
        },
        {
            title: '证件号码',
            dataIndex: 'sfzh'
        },
        {
            title: '是否在校',
            dataIndex: 'sfzx'
        },
        {
            title: '入学年份',
            dataIndex: 'rxnf'
        },
    ]
    // 操作栏通过权限判断是否显示
    /*if (hasPerm(['jwXsjbxxEdit', 'jwXsjbxxDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }*/
    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
        return jwXsjbxxApi.jwXsjbxxPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
	// 同步
	const impUploadLoading = ref(false)
	const impAlertStatus = ref(false)
	const impResultData = ref({})
	const impResultErrorDataSource = ref([])
	 async function syncupd  () {
		impUploadLoading.value = true
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState));
		await (jwXsjbxxApi.jwXsjbxxSyncupd(searchFormParam)).then((res) => {
			impAlertStatus.value = true
			impResultData.value = res
			impResultErrorDataSource.value = res.errorDetail
		})
			.finally(() => {
				impUploadLoading.value = false
			})
	}
	// 关闭同步提示
	const onImpClose = () => {
		impAlertStatus.value = false
	}
    // 删除
    const deleteJwXsjbxx = (record) => {
        let params = [
            {
                xh: record.xh
            }
        ]
        jwXsjbxxApi.jwXsjbxxDelete(params).then(() => {
            table.value.refresh(true)
        })
    }
    // 批量删除
    const deleteBatchJwXsjbxx = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                xh: m
            }
        })
        jwXsjbxxApi.jwXsjbxxDelete(params).then(() => {
            table.value.clearRefreshSelected()
        })
    }
</script>
